
"use strict";

angular.module('app.jurisdiction').controller('RoleCtrl', function ($scope,$http,$cookieStore) {
	//配置分页基本参数
	$scope.paginationConf = {
	    currentPage: 1,
	    itemsPerPage: 10,
	    pagesLength: 5,
	    totalItems: 20,
	    perPageOptions: [10, 20, 30, 40, 50],

	    onChange: function () {
	    }
	};
    
	
   	//1.列表
	$scope.select = function(){
		// $http.get(baseurls+"/list_nopage/role").then(function(res){
		// 	if(res.data.ret == 1){
		// 		// console.log(res.data.info);
		// 		$scope.roles = res.data.info;
		// 		$scope.paginationConf.totalItems = res.data.info.length;
		// 	}
		// })
		$http.post(baseurls+"/list/select/role", $scope.paginationConf).then(function(res){
            // console.log(res.data)
            if(res.data.ret == 1){
                $scope.roles = res.data.rows;
				$scope.paginationConf.totalItems = res.data.records;
				
				$http.get(baseurls+"/select_idname/project").then(function(res1){
					// console.log(res.data);
					if(res1.data.ret == 1){
						$scope.projects = res1.data.info;
						// console.log($scope.projects)
					}
				})
				
				for(var i=0;i<$scope.roles.length;i++){
					//角色权限文字化
					// console.log(JSON.parse($scope.roles[i].power));
					var pow = JSON.parse($scope.roles[i].power);
					if(pow == 0){
						$scope.roles[i].power = "0";
					}else{
						$scope.roles[i].power = "";
						for(var index in pow){
							// console.log(pow[index].projectid,pow[index].power)
							if(pow[index].power != "0"){
								for(var p=0;p<$scope.projects.length;p++){
									if($scope.projects[p].id == pow[index].projectid){
										$scope.roles[i].power += $scope.projects[p].name + ":" ;
										if(parseInt(pow[index].power) & 4) $scope.roles[i].power += "查看，";
										if(parseInt(pow[index].power) & 2) $scope.roles[i].power += "设备，";
										if(parseInt(pow[index].power) & 1) $scope.roles[i].power += "项目，";

										$scope.roles[i].power = $scope.roles[i].power.replace(/，$/,"。");
										//项目id --> 项目名称
										$scope.roles[i].project_name = $scope.projects[p].name;
										
									}
								}
							}
						}
					}
				}
            }
            
        })
	}
	$scope.select();
	//2.添加
	$scope.insert = {};
	$scope.insertdata = function () {
		$scope.insert.fid = $cookieStore.get("user");
		$scope.power = 0;
		$http.post(baseurls+"/data/role",$scope.insert).then(function(res){
			if(res.data.ret == 1){
				// console.log($scope.insert);
				new Notify('success','添加角色成功');
			}else{
				new Notify('fail','添加角色失败');
			}
			$("#addModal").modal("hide");
			$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);//更新列表
		})
	}
	//3.编辑用户
	$scope.editdata = {};
	$scope.showEditDevice = function (data) {
		$scope.editdata.id = data.id;
	    $scope.editdata.name = data.name;
	    $scope.editdata.power = data.power;
	    $scope.editdata.projectid = data.projectid;
	    $scope.editdata.fid = data.fid;
	    $scope.editdata.ifuse = data.ifuse;
	    $scope.editdata.des = data.des;

	    $('#editModal').modal('show');
	}
	$scope.updatedata = function () {
		$http.put(baseurls+"/data/role?id=="+$scope.editdata.id,$scope.editdata).then(function(res){
			if(res.data.ret == 1){
				new Notify('success','角色信息修改成功');
			}else{
				new Notify('fail','角色信息修改失败');
			}
			$("#editModal").modal("hide");
			$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);
		})
	}
	//4.删除用户
	$scope.delid = {};
	$scope.showDelDialog = function (id) {
	    $scope.delid = id;
	    $('#showDelDialog').modal('show');
	}
	$scope.deldevice = function () {
		$http.delete(baseurls+"/data/role?id=="+$scope.delid).then(function(res){
			if(res.data.ret == 1){
				new Notify('success','删除角色成功');
			}else{
				new Notify('fail','删除角色失败');
			}
			$('#showDelDialog').modal('hide');
			$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);
		})		
	}


});